package com.augurit.gzcgwsp.rqqyjyxk.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwPrintRqRqssylb;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwRqQyjyxk;
import com.augurit.gzcgwsp.ssylb.entity.YwRqRqssylb;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;

@Repository
public class YwRqQyjyxkDao extends BaseDao<YwRqQyjyxk, Long> {
	private final static Log log=LogFactory.getLog(YwRqQyjyxkDao.class);
    private static final Long[] Object = null;
    
	public YwRqQyjyxk getEntity(Long id) {
		YwRqQyjyxk entity=null;
		Session session=this.getSession();
		Criteria criteria=session.createCriteria(YwRqQyjyxk.class);
		criteria.add(Restrictions.eq("id",id));
		List<YwRqQyjyxk> list=criteria.list();
		if(list!=null && list.size()>0){
		    entity=list.get(0);
		}
		return entity;
		}
	

	public YwRqQyjyxk getYwRqQyjyxk(Long id,String property) {
		Session session=this.getSession();
		Criteria criteria=session.createCriteria(YwRqQyjyxk.class);
		criteria.add(Restrictions.eq(property, id));
		List list=criteria.list();
		if(list!=null &&list.size()>0)
			return (YwRqQyjyxk) list.get(0);
		return null;
	}

	/**
	 * 查询燃气设施
	 * @param id
	 * @return
	 */
	public List<YwRqRqssylb> getRqssylbs(Long id,Long qyid) {
		String hql2 = "from YwRqRqssylb t where t.qyjyxkid in( select id from YwRqQyjyxk where qyid="+qyid+" ) and t.sslb!='供应站' " +
				" and t.zdlb='合格' order by t.ssylbid asc";
		List<YwRqRqssylb> rqssylbs=this.find(hql2);
		return rqssylbs;
	}
	public List<YwRqRqssylb> getRqssylbs(Long qyjyxkid) {
		String hql2 = "from YwRqRqssylb t where t.qyjyxkid="+qyjyxkid+" and  t.zdlb='合格'" +
				"  order by t.ssylbid asc";
		List<YwRqRqssylb> rqssylbs=this.find(hql2);
		return rqssylbs;
	}
	/**
	 * 查询瓶装燃气供应站记录
	 * @param id
	 * @return
	 */
	
	public List<YwRqRqssylb> getRqgysByqyId(Long qyid,Long jyxkid) {
		String hql = " from YwRqRqssylb t where t.qyjyxkid in (select id from YwRqQyjyxk"+		
			" where qyid="+qyid+") and t.sslb='供应站' and t.zdlb='合格' order by t.ssylbid asc";
		List<YwRqRqssylb> list=this.find(hql);
		return list;
	}

	public List<YwRqRqssylb> getRqgysByqyId(Long qyid,String jyxkid) {
		String hql = " from YwRqRqssylb t where t.qyjyxkid in (select id from YwRqQyjyxk"+		
			" where qyid="+qyid+") and t.sslb='供应站' and t.zdlb='合格' order by t.ssylbid asc";
		List<YwRqRqssylb> list=this.find(hql);
		return list;
	}


	public YwRqRqssylb getRqjszdjbByName(String mc) {
		String hql = " from YwRqRqssylb t where t.ssmc='"+mc+"'"+		
			" and t.sslb='供应站' and t.zdlb='合格' order by t.ssylbid asc";
		List<YwRqRqssylb> list=this.find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
			
	}


	public YwRqQyjyxk getYwRqQyjyxkById(Long id, String acceptNo) {
		Session session=this.getSession();
		Criteria criteria=session.createCriteria(YwRqQyjyxk.class);
		criteria.add(Restrictions.eq("qyid", id));
		criteria.add(Restrictions.eq("acceptNo", acceptNo));
		List list=criteria.list();
		if(list!=null &&list.size()>0)
			return (YwRqQyjyxk) list.get(0);
		
		return null;
	}


	/**
	 * 查询企业信息
	 * @param qyid
	 * @return
	 */
    public YwComQyxx getYwRqQyjyxkById(Long qyid) {
        Session session=this.getSession();
        Criteria criteria=session.createCriteria(YwComQyxx.class);
        criteria.add(Restrictions.eq("id", qyid));
        List list=criteria.list();
        if(list!=null &&list.size()>0)
            return (YwComQyxx) list.get(0);
        return null;
    }


    /**
     * 查询企业是第几家企业 主要是查询rownum,此方法需要根据id循环找出rownum
     * 如果数量较大会影响性能
     * @param id 
     * @return
     */
    public Long getQyNum(Long id) {
        Long num=0l;
        String hql = "select rownum,id from "+YwRqQyjyxk.class.getName();
        List<Object[]> list=this.find(hql);
        if(id!=null){
            if(list!=null && list.size()>0){
                for(Object[] objs:list){
                    Long idobj=(Long) objs[1];
                    if(String.valueOf(idobj).equals(String.valueOf(id))){
                        num=(Long) objs[0];
                    }
                }
            }
        }
      
        return num;
    }


    /**
     * 
     * @param id当前案件的id
     * @param qyid 企业id
     * @return
     */
	public List<YwPrintRqRqssylb> getPrintRqssylbs(Long id, Long qyid,boolean isGyz) {
		String hql="";
		if(isGyz){
			 hql = "from YwPrintRqRqssylb t where t.qyjyxkid ="+id +" and t.sslb='供应站' order by ssylbid asc";

		}else{
			 hql = "from YwPrintRqRqssylb t where t.qyjyxkid ="+id +" and t.sslb!='供应站' order by ssylbid asc";
		}
		
		List<YwPrintRqRqssylb> rqssylbs=this.find(hql);
		return rqssylbs;
	}



	
}